package cn.wokoba.dao.mapper;


import cn.wokoba.dao.pojo.RaffleActivityAccountMonth;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

//抽奖活动账户表-月次数
@Mapper
public interface RaffleActivityAccountMonthMapper extends BaseMapper<RaffleActivityAccountMonth> {
    @Update("""
             update raffle_activity_account_month
                    set month_count_surplus = month_count_surplus - 1, update_time = now()
                    where user_id = #{userId} and activity_id = #{activityId} and month = #{month} and month_count_surplus > 0\
            """)
    int updateActivityAccountMonthSubtractionQuota(RaffleActivityAccountMonth build);

    @Insert("""
            insert into raffle_activity_account_month(
            user_id, activity_id, month, month_count, month_count_surplus, create_time, update_time)
            values (
            #{userId}, #{activityId}, #{month}, #{monthCount}, #{monthCountSurplus}, now(), now())\
            """)
    void insertActivityAccountMonth(RaffleActivityAccountMonth build);

    @Update("""
              update raffle_activity_account_month
                    set month_count_surplus = month_count_surplus + #{monthCountSurplus},
                        update_time = now()
                    where user_id = #{userId} and activity_id = #{activityId} and month = #{month}\
            """)
    void addAccountQuota(RaffleActivityAccountMonth raffleActivityAccountMonth);

    @Select("""
                    select user_id, activity_id, month, month_count, month_count_surplus
                    from raffle_activity_account_month
                    where user_id = #{userId} and activity_id = #{activityId} and month = #{month}\
            """)
    RaffleActivityAccountMonth queryActivityAccountMonthByUserId(RaffleActivityAccountMonth build);
}
